
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>购房资格测试器</title>
    <script src="http://libs.baidu.com/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>

                        <div id="QuestionList" class="question-list"></div>
                    <div class="step-action">
                        <a id="BtnPre" class="btn gray-btn" href="javascript:;">上一步</a>
                        <a id="BtnNext" class="btn blue-btn" href="javascript:;">下一步</a>
                    </div>
    <script type="text/javascript" src="static/data.json"></script>
    <script type="text/javascript">
 var fangdai = {
    els: {
        //问题列表
        qList: document.getElementById("QuestionList"),
        //下一步
        next: $("#BtnNext"),
        //上一步
        pre: document.getElementById("BtnPre"),
        //专家提示
    },
    init: function(){
        var me = this;
        //绑定下一步、上一步操作事件
        me.bindAction();
        fangdai.drawList(fangdai.getCity());
        //location.href = "/done";
    },  
    //绑定下一步、上一步事件操作
    bindAction: function(){
        var me = this;
        me.els.next.click(function(){
            var _selRadio = $('input[name=' + me.varList.varName + ']:checked');
            var _value = _selRadio.val();
            var _text = _selRadio.attr("desc");
            me.next(_value, _text);
        });
        me.els.pre.click(function(){

        });
    },
    //下一步执行
    next: function(id,text){
        console.log(id);
        console.log(text);
        var me = this;
        if(me.varList.varName == "city_id"){
            me.varList.cityId = id;
        }else{
            //追加计算结果列表
            me.resultList[me.varList.eleId] = {
                id: me.varList.eleId,
                value: id,
                varName: me.varList.varName,
                varText: text,
                title: me.varList.title,
            };
        }

        var result = me.calcu();
        //如果是继续
        if(result.type == "goon"){
            return me.getElementList();
            //me.getElementList();
        }           
        //如果是成功
        if(result.type == "success"){
            var _result = 0;
            //如果成功后，则转身num_have处理逻辑
            if(me.varList.token == "loan_index"){
                me.statusList.nowCount.key = true;
                me.statusList.nowCount.data = result.value;
            }
            if(me.varList.token == "num_have"){
                me.statusList.nowHave.key = true;
                me.statusList.nowHave.data = result.value;
                me.varList.token = "loan_index";
            }
            if(me.varList.token == "can_buy"){
                me.statusList.canBuy.key = true;
                me.statusList.canBuy.data = result.value;
                me.varList.token = "num_have";
            }
            //console.log("状态列表：" + JSON.stringify(me.statusList));
            if(me.statusList.nowCount.key == true){
                console.log(me.resultList);
                console.log("你可以购买：" + (me.statusList.canBuy.data - me.statusList.nowHave.data));
                console.log("此次购买算做第" + (me.statusList.nowCount.data) + "套房");
            }else{
                me.calcu();
                return me.getElementList();
            }
        }
    },
    //数据分层
    dataList:{
        //城市列表
        city: data.hpcity,
        //选项列表
        element: data.city_element,
        //政策列表
        policy: data.city_policy
    },
    //状态列表
    statusList: {
        //能买几套房
        canBuy: {
            key: false,
            data: 0
        },
        //拥有几套房
        nowHave: {
            key: false,
            data: 0
        },
        //此次购买算第几套
        nowCount: {
            key: false,
            data: 0
        }
    },
    //数据变量
    varList: {
        token: "can_buy",
        cityId: "10000",
        varName: "city_id",
        varValue: "",
        varText: "",
        title: "",
        eleId: "",
        canBuy: 0
    },
    //计算结果数据结构
    resultList:{},
    /*
        {
            id: "",步骤id
            title: "",//标题描述
            answer: ""//回答值
        }
    */
    //获取城市列表
    getCity: function(){
        var _result = {
            title: "您所在的城市",
            data: []
        };
        var city = data.hpcity;
        for(var index = 0;index < city.length; index++){
            _result.data.push({
                value: city[index].id,
                desc: city[index].name
            });
        }
        window.cityData = JSON.stringify(_result);
        this.varList.varName = "city_id";
        return _result;
    },
    //获取枚举列表
    getElementList: function(){
        //console.log(id);
        var me = this;
        //计算
        id = me.varList.eleId;
        cityid = this.varList.cityId;
        var elementData = this.dataList.element;
        var _result = {
            title: "",
            data: []
        };

        for(var index = 0;index < elementData.length; index++){
            if(elementData[index].id == id && elementData[index].city_id == cityid){
                //console.log("title:" + item.title);
                _result.title = elementData[index].title;
                _result.data = elementData[index].data;
                me.varList.varName = elementData[index].var_name;
            }
        }
        //console.log(!_result.data);
        if(_result.data.length == 0){
            for(var index2 = 0;index2 < elementData.length; index2++){
                if(elementData[index2].id == id && elementData[index2].city_id == "10000"){
                    //console.log("title:" + item.title);
                    _result.title = elementData[index2].title;
                    _result.data = elementData[index2].data;
                    me.varList.varName = elementData[index2].var_name;
                }
            }
        }
        //console.log(_result);
        me.varList.title = _result.title;
        this.drawList(_result);
        return _result;
    },
    //获取下一个问题id
    getNextQ: function(data){
        var me = this;
        token = me.varList.token;
        var branch = {};
        //计算问题出现数
        for(var index = 0;index < data.length; index++){
            //console.log(item);
            for(var subIndex = 0;subIndex < data[index].length; subIndex++){
                var subitem = data[index][subIndex];
                //console.log(subitem);
                if(branch[subitem.id]){
                    branch[subitem.id] += 1;
                }else{
                    branch[subitem.id] = 1;
                }
            };
        };
        //步骤id按出现次数排序
        var _dataList = [];
        for(var key in branch){
            _dataList.push({
                id: key,
                data: branch[key]
            });
        }
        _dataList.sort(function(a,b){return a["data"]>b["data"]?1:-1});
        _dataList.reverse();
        //console.log("resultList:" + JSON.stringify(me.resultList));
        for(var i = 0;i<_dataList.length; i++){
            //console.log("resultID:" + _dataList[i].id);
            if(!me.resultList[_dataList[i].id]){
                //返回问题id
                return _dataList[i].id;
            }
        }
    },
    //计算结果逻辑
    calcu: function(){
        var me = this;
        token = me.varList.token;
        var branch = {};
        var policyList = this.dataList.policy;
        var policy;
        var _resultData = {};
        var _passKey = false;
        //输出
        var _result = {
            type: "",
            value: ""
        };

        //console.log(me.varList.cityId);
        //获取匹配城市项
        for(var i = 0;i<policyList.length; i++){
            //匹配城市ID和token
            if((me.varList.cityId == policyList[i].city_id) && (token == policyList[i].token)){
                policy = policyList[i];
            }
        };
        //如果没有找到匹配项，则按默认匹配
        if(!policy){
            for(var n = 0;n<policyList.length; n++){
                //匹配城市ID和token
                if((me.varList.cityId == "10000") && (token == policyList[n].token)){
                    policy = policyList[n];
                }
            };

        }
        var emptyItemList = [];
        //计算结果
        //$(policy.data).each(function(index, item){
        for(var index = 0;index < policy.data.length; index++){
            var item = policy.data[index];
            console.log(item);
            var _branchResult = true;
            //console.log("relength:" + item.require.length);

            for(var subindex = 0; subindex<item.require.length; subindex++){
                var subitem = item.require[subindex];
                if(me.resultList[subitem.id]){
                    console.log("data：" + subitem.data);
                    //console.log(me.resultList[subitem.id]);
                    if( subitem.data.indexOf(me.resultList[subitem.id].value) == -1){
                        //console.log("失败");
                        _branchResult = false;
                        break;
                    }
                }
                else if(_branchResult == true){
                    _branchResult = 'empty';
                }
            }
            //当计算成功后
            if(_branchResult == true){
                //console.log(item.result_val);
                _result = {
                    type: "success",
                    value: item.result_val
                };

                return _result;
            }else if(_branchResult == 'empty'){
                emptyItemList.push(item);
            }

        }
        //如果失败
        if(emptyItemList.length == 0){
            _result = {
                type: "fail",
                value: "您不能买房子"
            };

            return _result;
        }
        
        //如果继续计算
        var _eleId = me.getNextQ(emptyItemList);
        me.varList.eleId = _eleId;
        _result = {
            type: "goon",
            value: me.varList.eleId
        };

        return _result;
    },
    //渲染列表
    drawList: function(data){
        //console.log(data);
        var me = this;

        var _listDom = "";
        var _cityOther = "";
        for(var i = 0;i<data.data.length; i++){
            var item = data.data[i];
            var _itemDom = me.tpl.pListItem
                            .replace("{name}", me.varList.varName)
                            .replace("{value}", item.value)
                            .replace(/{text}/g, item.desc);
            _listDom += _itemDom;
        };
        //console.log(data.data);
        me.els.qList.innerHTML =(_listDom);
        return data.data;
    },
    //模板
    tpl: {
        //问题列表模板
        pListItem: '<p{title}><label><input type="radio" name="{name}" value="{value}" desc="{text}" /></label><span class="label-text">{text}</span></p>'
    }
};

fangdai.init();
    </script>
</body>
</html>